package com.bdqn.tour.servlet.Gyh;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证码
 */

@WebServlet("/valSmsCode")
public class GyhValSmsCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain;charset=utf-8");
        //获取手机号码
        String phone = req.getParameter("phone");
        String valCode = req.getParameter("valCode");

        //会话中获取值 ，进行比较手机号码
        String  sessionValCode = (String)req.getSession().getAttribute(phone);

        PrintWriter out = resp.getWriter();
        Map<String,Object> map = new HashMap<>();
        //发送短信
        if (valCode.equals(sessionValCode)) {
            map.put("code","0000");
//            req.getSession().removeAttribute(phone);
        }else{
            map.put("code","2001");
        }
        //移除会话中的共享的code
        //转成字符串
        String json = JSON.toJSONString(map);
        out.print(json);
        out.close();

    }
}
